package com.aqie.meet.ui.login;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import com.aqie.libcommon.app.BaseActivity;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.widget.CodeCount;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 手机号 验证码
 */
public class RegActivity extends BaseActivity {

    @BindView(R.id.bt_next)
    Button mButtonNext;

    @BindView(R.id.et_phone)
    EditText mPhone;
    @BindView(R.id.et_code)
    EditText mCode;

    @BindView(R.id.tv_code)
    Button mGetCode;


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_reg;
    }

    @OnClick(R.id.bt_next)
    void next(){
        String phone = mPhone.getText().toString();
        String code = mCode.getText().toString();
        if (TextUtils.isEmpty(mPhone.getText().toString()) || TextUtils.isEmpty(mCode.getText().toString())) {
            showError("手机号/验证码不能为空！");
            return;
        }
        Intent intent = new Intent(this, Reg2Activity.class);
        intent.putExtra("phone", phone);
        intent.putExtra("code", code);
        startActivity(intent);
    }

    CodeCount count = null;
    @OnClick(R.id.tv_code)
    public void sendSms(){
        if (TextUtils.isEmpty(mPhone.getText().toString())) {
            showError("手机号不能为空！");
            return;
        }
        count = new CodeCount(mGetCode, 60000, 1000);
        count.start();
        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<String>> call = service.smsLogin(mPhone.getText().toString(), 2);
        call.enqueue(new Callback<RspModel<String>>() {
            @Override
            public void onResponse(Call<RspModel<String>> call, Response<RspModel<String>> response) {
                if(response.body()!=null && response.body().success()){
                    Log.e(TAG, "sms:"+ response.body().getData());

                    BaseApplication.showToast("验证码发送成功");
                } else {
                    if(response.body() != null){
                        showError(response.body().getMsg());
                    }else{
                        showError("验证码发送失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<RspModel<String>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
                showError("验证码发送失败");
            }
        });
    }


    public void showError(String errMsg) {
        BaseApplication.showToast(errMsg);
        mPhone.setEnabled(true);
        mCode.setEnabled(true);
        mButtonNext.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (count != null) {
            count.cancel();
        }
    }
}